var api = require('../../../utils/api.js')
var $ = require('../../../utils/util.js')
var regeneratorRuntime = require('../../../utils/runtime.js')
var app = getApp()
var Store = app.Store 

Page({
  data: {
    operateTypeList:['变压器检修', '高压柜检修','进线线缆检修'],
    operateType:'变压器检修',
    workType:[],
    typeRemark:'',
    workRisk:[],
    riskRemark:'',
    measures:[],
    perControl:[],
    controlRemark:'',
    user_name: 0, // 用户id(告警独有)
    responsibleImg: 'https://img.yzcdn.cn/vant/cat.jpeg', // 签发人签字
    deviceName:[],
    deviceId:'',
    signImg:'',
    ticketInfo:{},
    type: 2, // 类型
  },
  onLoad: function (options) { 
    Store.dispatch({
      type: 'SET_FROM',
      form: {
      }, 
    })

    if ('ticketInfo' in options){ 
      this.setData({
        ticketNumber:JSON.parse(options.ticketInfo).ticketNumber, 
        ticketInfo:JSON.parse(options.ticketInfo),
      })
      this.getSafetyRisk()
    }
  },

  onShow(){
    var form = Store.getState().formInfor.form
    var key = form.key
    this.setData({
      signImg: wx.getStorageSync('signUrl'),
    })
    if (!form.selected) return
    switch (key) {
      case 'selectWorkType': // 作业类别  
        this.setData({
          workType: form.selected,
        })
        break
      case 'selectWorkRisk': // 作业风险  
        this.setData({
          workRisk: form.selected,
        })
        break 
      case 'selectPerControl': // 风险控制  
        this.setData({
          perControl: form.selected,
        })
        break
      case 'riskMeasures': // 获取安措3  
        const msu=this.data.measures
        const idx=msu.findIndex((obj)=>(obj.operationProcess==form.selected.operationProcess&&obj.riskDescription==form.selected.riskDescription))
        idx!=-1? msu[idx] = form.selected:msu.push(form.selected)
        this.setData({
          measures:msu,
        })
        break 
      case 'workSign': // 获取签发人签名  
        this.setData({
          signImg: form.selected,
        })
        break 
        
    }
    console.log(form,this.data.signImg) 
  },
  selectOperateType(){
    var that=this
    wx.showActionSheet({
      itemList: that.data.operateTypeList,
      success (res) {
        console.log(res)
        that.setData({
          "operateType":that.data.operateTypeList[res.tapIndex]
        })
        that.getSafetyRisk()
      },
      fail (res) {
        console.log(res.errMsg)
      }
    })
  },
  getSafetyRisk(){
    api.$get(api.workTicket.safetyRiskCard, {
      ticketNumber:this.data.ticketNumber,
      operationCategory:this.data.operateType
    }).then((res) => {
      console.log(res.data.safetyRiskControlCard.workCategory.split(',')) 
      if(res.code==200){
        this.setData({
          safetyRiskControl:res.data.safetyRiskControlCard,
          workType:res.data.safetyRiskControlCard.workCategory.split(','),
          typeRemark:res.data.safetyRiskControlCard.workCategorySupplement,
          workRisk:res.data.safetyRiskControlCard.workRisks.split(','),
          riskRemark:res.data.safetyRiskControlCard.workRiskSupplement,
          perControl:res.data.safetyRiskControlCard.processRiskControl.split(','),
          measures:res.data.riskControllerTaskList
        }) 
      }
    })
  },
  selectWorkType(){
    Store.dispatch({
      type: 'SET_FROM',
      form: {
        key: 'selectWorkType', // 后台要的更改字段 
        type: 2, // 表单类型 1： radio  2：select
        config: {
          deviceId:'001'
        },
      },
    })
    $.linkurl(`/pages/main/chooseForm/chooseForm`)
  },
  selectWorkRisk(){
    Store.dispatch({
      type: 'SET_FROM',
      form: {
        key: 'selectWorkRisk', // 后台要的更改字段 
        type: 2, // 表单类型 1： radio  2：select
        config: {
          deviceId:'001'
        },
      },
    })
    $.linkurl(`/pages/main/chooseForm/chooseForm`)
  },
  selectPerControl(){
    Store.dispatch({
      type: 'SET_FROM',
      form: {
        key: 'selectPerControl', // 后台要的更改字段 
        type: 2, // 表单类型 1： radio  2：select
        config: {
          deviceId:'001'
        },
      },
    })
    $.linkurl(`/pages/main/chooseForm/chooseForm`)
  },
  getMeasures(e){
    const {item,type}=$.detail(e)
    const taskList=this.data.measures?.find((obj)=>{
      return obj.operationProcess==type&&obj.riskDescription==item
    })||{}
    Store.dispatch({
      type: 'SET_FROM',
      form: {
        key: 'riskMeasures', // 后台要的更改字段 
        config: {
          riskType:item,
          mainType:type,
          riskLevel:taskList.hasOwnProperty('list')?taskList.list[0].riskLevel:1, 
          riskControl: taskList.hasOwnProperty('list')?taskList.list:[],
          ticketNumber:this.data.ticketNumber,
          operateType:this.data.operateType,
        },
      },
    })
    $.linkurl(`/pages/workTicket/workRiskMessure/workRiskMessure`) 
  },
  remarkIpt(e) {
    this.setData({
      [e.currentTarget.dataset.key]: e.detail.value
    })
  },
  additionalMeasures(e){
    this.setData({'safe.additional':e.detail.value})
  },
  beginSign(e){  
    var that=this
    if(!Boolean(that.data.signImg)){
      Store.dispatch({
        type: 'SET_FROM',
        form: {
          key: 'workSign', 
        },
      })
      $.linkurl(`/pages/components/signatrue/signAture?ticketNumber=${that.data.ticketNumber}`) 
    }
    // wx.showModal({
    //   title: '提示',
    //   content: '使用原有签名或者手动签名',
    //   confirmText:'原有签名',
    //   cancelText:'手动签名',
    //   success (res) {
    //     if (res.confirm) {
          
    //       that.setData({
    //         signImg: wx.getStorageSync('signUrl'),
    //       })
    //     } else if (res.cancel) {
    //       Store.dispatch({
    //         type: 'SET_FROM',
    //         form: {
    //           key: 'workSign', 
    //         },
    //       })
    //       $.linkurl(`/pages/components/signatrue/signAture?ticketNumber=${that.data.ticketNumber}`) 
    //     }
    //   }
    // })
  },
  bigSign(e){
    const {img}=$.detail(e)
    wx.previewImage({
      current: '', // 当前显示图片的链接，不填则默认为urls的第一张
      urls: [img] // 需要预览的图片链接列表
    })
  },
  cancelBack(){
    $.linkurl('','back')
  },
  subRiskCard(){
    if(!Boolean(this.data.signImg)){
      return $.toast('当前无默认签名，请手动填写！')
    }
    api.$post(api.workTicket.addSafetyRiskCard,{
      ticketNumber:this.data.ticketNumber,
      riskControllerTaskList:this.data.measures,
      safetyRiskControlCard: {
        ...this.data.safetyRiskControl,
        "workCategory": this.data.workType.join(','),
        "workCategorySupplement": this.data.typeRemark,
        "workRisks": this.data.workRisk.join(','),
        "workRiskSupplement": this.data.riskRemark,
        "processRiskControl": this.data.perControl.join(','),
        "workLeaderSignature":this.data.signImg,
    }
    }).then((res) => {
      api.$get(api.workTicket.riskLeaderConfirm,{
        ticketNumber:this.data.ticketNumber,
        signature:this.data.signImg,
        approvalResult:true,
      }).then((res2) => {
        // console.log(res2)
        if(res2.code==200){
          $.toast('提交成功')
          $.linkurl(``,'back')
        }
      })
    })
  }





})